<?php
namespace App\Repositories;

use App\Helper\Utils;
use App\Traits\SingletonTrait;
use Hyperf\DbConnection\Db;
class CmsPostRepository extends AbstractRepository
{
    use SingletonTrait;
    public function model()
    {
        return 'App\\Model\\CmsPost';
    }

    public function store($data)
    {
        $model = isset($data['id'])&&(int)$data['id']?$this->getById($data['id']):new $this->model();
        $model->fill($data);
        DB::beginTransaction();
        try{
            if(!$model->save()){
                throw new \Exception('数据保存失败');
            }
            DB::commit();
        }catch (\Throwable $exception){
            DB::rollBack();
            return $this->error($exception->getMessage());
        }
        return $this->success($model);
    }
}